import G6 from '@antv/g6'
import { hexToRgba } from '@/utils/color.js'

export default function () {
  G6.registerEdge(
    'baseEdge',
    {
      /**
       * 默认配置
       */
      getDefaultOptions() {
        return {
          style: {
            stroke: '#000',
            lineWidth: 5,
          },
          labelCfg: {
            autoRotate: true,
            style: {
              fill: '#595959',
              fontSize: 12,
              background: {
                fill: '#ffffff',
                stroke: '#e2e2e2',
                padding: [4, 8, 4, 8],
                radius: 4,
              },
            },
          },
        }
      },

      /**
       * 合并配置
       * @param {Object} cfg - 外部传入的配置
       * @returns {Object} - 合并后的配置
       */
      getMergedOptions(cfg) {
        const defaultOptions = this.getDefaultOptions()
        if (!cfg) return defaultOptions

        const { style: cfgStyle = {}, labelCfg: cfgLabelCfg = {} } = cfg
        return {
          style: {
            ...defaultOptions.style,
            ...cfgStyle,
          },
          labelCfg: {
            ...defaultOptions.labelCfg,
            ...cfgLabelCfg,
            style: {
              ...defaultOptions.labelCfg.style,
              ...cfgLabelCfg.style,
            },
          },
        }
      },

      options: {
        style: {
          stroke: '#000',
          lineWidth: 5,
        },
        labelCfg: {
          autoRotate: true,
          style: {
            fill: '#595959',
            fontSize: 12,
            background: {
              fill: '#ffffff',
              stroke: '#e2e2e2',
              padding: [4, 8, 4, 8],
              radius: 4,
            },
          },
        },
      },

      setState(name, value, item) {
        const shape = item.get('keyShape')
        const model = item?.getModel() || {}
        const group = item?.getContainer()
        const children = group?.get('children')
        const edgeHighlight = children.find((item) => item.get('name') === 'edge-highlight')
        const label = children.find((item) => item.get('name') === 'text-shape')

        shape.attr('opacity', 1)
        edgeHighlight.attr('opacity', 0)
        if (label) {
          label.attr('opacity', 1)
        }

        // 清除之前可能存在的呼吸动画
        if (item._breathingAnimation) {
          item._breathingAnimation.stop()
          delete item._breathingAnimation
        }

        switch (name) {
          case 'dark':
            shape.attr('opacity', value ? 0.2 : 1)
            if (label) {
              label.attr('opacity', value ? 0.2 : 1)
            }
            break
          case 'highlight':
            edgeHighlight.attr('opacity', value ? 1 : 0)
            if (label) {
              label.attr('opacity', value ? 1 : 0)
            }
            break
          case 'breathing':
            if (value) {
              // 创建呼吸效果动画
              const breathe = () => {
                shape.animate(
                  {
                    lineWidth: model.style?.lineWidth * 1.5 || 7.5,
                    opacity: 0.8,
                  },
                  {
                    duration: 800,
                    easing: 'easeCubic',
                    callback: () => {
                      shape.animate(
                        {
                          lineWidth: model.style?.lineWidth || 5,
                          opacity: 0.4,
                        },
                        {
                          duration: 800,
                          easing: 'easeCubic',
                          callback: breathe,
                        }
                      )
                    },
                  }
                )
              }
              breathe()
              // 存储动画引用以便后续停止
              item._breathingAnimation = shape.get('animations')[0]
            } else {
              // 恢复正常状态
              shape.attr({
                lineWidth: model.style?.lineWidth || 5,
                opacity: 1,
              })
            }
            break
        }
      },

      afterDraw(cfg, group) {
        // 获取合并后的配置
        const options = this.getMergedOptions(cfg)

        // 初始化时隐藏标签
        const label = group.find((item) => item.get('name') === 'text-shape')
        if (label) {
          label.attr('opacity', 0)
        }

        const startPoint = cfg.startPoint
        const endPoint = cfg.endPoint
        group.addShape('path', {
          attrs: {
            path: [
              ['M', startPoint.x, startPoint.y],
              ['L', endPoint.x, endPoint.y],
            ],
            stroke: hexToRgba(cfg.color || options.style.stroke, 0.25),
            lineWidth: 11,
            opacity: 0,
            cursor: 'pointer',
          },
          name: 'edge-highlight',
          zIndex: 1,
        })
      },
    },
    'line'
  )
}
